from selenium.webdriver.common.by import By
from selenium import webdriver
import time

def switch_current_page(driver):
    """切回到当前页面"""
    windows = driver.window_handles
    driver.switch_to.window(windows[-1])
    driver.implicitly_wait(5)

def login(driver):
    """登录"""
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, '.login-qrcode__title__right').click()  # 点击账号登录
    driver.find_element(By.CSS_SELECTOR, '.js-tab-password-login').click()  # 密码登录
    driver.find_element(By.CSS_SELECTOR, ".account-input__input.zent-input--size-normal.zent-input-wrapper > .zent-input").clear()
    driver.find_element(By.CSS_SELECTOR, ".account-input__input.zent-input--size-normal.zent-input-wrapper > .zent-input").send_keys("15558172127")
    driver.find_element(By.CSS_SELECTOR, ".account-password__input.account-password__input--show-forget.zent-input--size-normal.zent-input-wrapper > input[name='password']").clear()
    driver.find_element(By.CSS_SELECTOR, ".account-password__input.account-password__input--show-forget.zent-input--size-normal.zent-input-wrapper > input[name='password']").send_keys("ag@12138")
    driver.find_element(By.CSS_SELECTOR, ".login-container__protocol__check.shake-motion.zent-checkbox-wrap > .zent-checkbox > input[type='checkbox']").click() # 同意协议
    time.sleep(0.5)
    driver.find_element(By.CSS_SELECTOR, ".js-login-btn.login-container__btn.zent-btn.zent-btn-block.zent-btn-border-transparent.zent-btn-large.zent-btn-primary").click()  # 点击登录
    driver.implicitly_wait(5)
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, "div[title='爱逛STUDIO-91186123']  .name-str").click()    # 点击爱逛studio店铺
    driver.implicitly_wait(10)
    time.sleep(8)

def uiStart():
    """开始"""
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.get('https://account.youzan.com/login')
    driver.maximize_window()
    time.sleep(2)
    driver.implicitly_wait(3)
    login(driver)
    return driver

def order_delivery(orderNo):
    """发货"""
    driver = uiStart()
    driver.find_element(By.CSS_SELECTOR, ".icondash_common_order_search.iconfont").click() # 点击订单查询
    switch_current_page(driver)
    driver.find_element(By.CSS_SELECTOR, ".zent-input--size-normal.zent-input-wrapper > input[name='keyword']").send_keys(orderNo)
    driver.find_element(By.CSS_SELECTOR, ".zent-btn.zent-btn-primary > span").click()   # 点击搜索订单
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".operation-cell .zent-btn > span").click() # 点击发货
    time.sleep(2)
    driver.find_element(By.CSS_SELECTOR, ".tr > .cell.cell--selection input[type='checkbox']").click()  # 点击选中一个商品
    driver.find_element(By.CSS_SELECTOR, "label:nth-of-type(3) > .zent-radio > input[type='radio']").click()    # 点击无需物流
    driver.find_element(By.CSS_SELECTOR, ".action-area.zent-form.zent-form--horizontal .zent-btn.zent-btn-primary > span").click()  # 点击发货
    time.sleep(1)

def order_refund(orderNo):
    """同意退款"""
    driver = uiStart()
    driver.find_element(By.CSS_SELECTOR, ".icondash_common_order_refunds.iconfont").click()  # 点击退款维权
    switch_current_page(driver)
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".zent-dialog-r-anchor [data-zv] .zent-btn:nth-of-type(1)").click()
    driver.find_element(By.CSS_SELECTOR, "div:nth-of-type(1) > ._1S5L > ._3MpH._3wam.zent-input--size-normal.zent-input-wrapper > .zent-input").send_keys(orderNo)
    driver.find_element(By.CSS_SELECTOR, ".wv5N.zent-btn.zent-btn-primary").click()   # 点击筛选
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".Z4Tt.zent-col.zent-col-2 > ._2Z7l").click()    # 点击处理退款
    switch_current_page(driver)
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, ".zent-btn.zent-btn-primary > span").click()   # 点击同意仅退款
    time.sleep(0.5)
    driver.find_element(By.CSS_SELECTOR, ".complaint-dialog.zent-dialog-r.zent-zoom-appear-done.zent-zoom-enter-done .zent-btn.zent-btn-primary > span").click()  # 点击确认退款
    time.sleep(3)




if __name__ == "__main__":
    order_refund('E20220621203352026200107')
